<?php
class FileUploadView {
	private $upload = 'uploadFile';
	private $imgPath = 'upload/';
	private $imgName = '';
	
	const FILE = "file";
	const UPLOAD_IMAGE = "uploadImage";


	public function ReturnFilePath() {
		if (isset($_FILES[self::FILE]['name'])){
			return $this -> imgPath;
		}else{
			return "";
		}
	}

	public function ReturnFileName() {
		if (isset($_FILES[self::FILE]['name'])){
			return $_FILES[self::FILE]['name'];
		}else{
			return "";
		}
	}
	
	// Returns true/false whether the user has tried to log in or not
	public function TriedToUploadImage() {
		if (isset($_POST[self::UPLOAD_IMAGE])) {
			return true;
		} else {
			return false;
		}
	}

	// Return imageupload button to put in a form
	public function GetFileUploadInputs (){
		return "<p>Ladda upp bild:
			<input type='file' name ='".self::FILE."' value=''/></p>
	
			<input type='submit' name='".self::UPLOAD_IMAGE."' value='Ladda upp' >";
	}

	public function DoFileExistAlready() {
		if (file_exists($this -> imgPath . $_FILES["file"]["name"])) {
			return true;
		} else {
			return false;
		}
	}
	
	//If the filename already exists BuildFileName is your friend and help you get another name by adding a number. 
	private function BuildFileName($file, $i, $ext) {
		$base = basename($file, '.' . $ext);
		return $base . $i . "." . $ext;
	}
	
	//Same as above, they work together. 
	public function RenameFile() {
		$i = 1;
		$file = $this -> imgPath . $_FILES[self::FILE]["name"];
		$info = pathinfo($file);
		// gets information like dirname, basename, extension, and filename about file in array.
		$file_name = $this -> BuildFileName($file, "", $info['extension']);
		// Cuts extensions like ".jpg" away.

		while (file_exists($this -> imgPath . $file_name)) {// Add number to filename until "the file doesn't exist".
			$i++;
			$file_name = $this -> BuildFileName($file, $i, $info['extension']);
		}
		$_FILES[self::FILE]["name"] = $file_name;
	}

	//Returns feedback whether the upload did alright or not.
	public function ReturnXhtmlFeedback() {
		if ($this -> TriedToUploadImage()) {
			return "<p>uppladdningen lyckades</p>";
		} else {
			return "<p>Uppladdningen misslyckades</p>";
		}
	}

	//Returns a list with links with all the images in my imagefolder.
	public function ReturnXhtmlFileList($imgarray) {
		$ret = "\n<ul>";

		foreach ($imgarray as $e) {
			foreach ($e as $k) {
				$ret .= "\n<li><a href='#'>" . $k . "</a></li>";
			}
		}
		$ret .= "\n</ul>";
		return $ret;

		//TODO:SKRIV INTE UT FILÄNDELSEN
	}

	public function TestFileUploadView() {

		$xhtml = "<h1>Tester av FileUploadView</h1>";

		$xhtml .= "<p class='instruction'>Feedback för misslyckad uppladdning</p>";
		$xhtml .= $this -> ReturnXhtmlFeedback();
		
		$_POST[$this -> upload] = true;
		$xhtml .= "<p class='instruction'>Feedback för lyckad uppladdning</p>";
		$xhtml .= $this -> ReturnXhtmlFeedback();
		$_POST[$this -> upload] = false;

		return $xhtml;
	}

}
